﻿/////////////////////////////////////////////////////////////////////////////////////////////
// <copyright file="OnPremiseMailBox.cs" company="Microsoft Corporation">
//     Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
/////////////////////////////////////////////////////////////////////////////////////////////

namespace Microsoft.LiveAtEdu.Samples.Powershell.OnPremise
{
    using System;
    using System.Collections.ObjectModel;
    using System.Management.Automation;

    /// <summary>
    /// Powershell commands for working with onpremise mailbox.
    /// </summary>
    public class OnPremiseMailBox
    {
        /// <summary>
        /// SessionRunspace instance.
        /// </summary>
        private readonly SessionRunspace sessionRunspace;

        /// <summary>
        /// Initializes a new instance of the OnPremiseMailBox class.
        /// </summary>
        /// <param name="sessionRunspace">SessionRunspace instance.</param>
        public OnPremiseMailBox(SessionRunspace sessionRunspace)
        {
            if (sessionRunspace == null)
            {
                throw new ArgumentOutOfRangeException("sessionRunspace");
            }

            this.sessionRunspace = sessionRunspace;
        }

        /// <summary>
        /// Creates a mailbox in on-premise exchange. Also, this automatically creates an AD account.
        /// </summary>
        /// <param name="name">User display name.</param>
        /// <param name="alias">User alias.</param>
        /// <param name="organizationUnit">Name of the organizational unit.</param>
        /// <param name="upnName">User principal name.</param>
        /// <param name="samAccountName">SAM account name.</param>
        /// <param name="firstName">User first name.</param>
        /// <param name="middleInital">User middle initial.</param>
        /// <param name="lastName">User last name.</param>
        /// <param name="password">User password.</param>
        /// <param name="userRole">User role.</param>
        /// <param name="windowsLiveLogin">Windows live login.</param>
        /// <returns>Return true if a mailbox is created.</returns>
        public bool CreateOnPremiseExchangeMailBox(
            string name,
            string alias,
            string organizationUnit,
            string upnName,
            string samAccountName,
            string firstName,
            string middleInital,
            string lastName,
            string password,
            string userRole,
            string windowsLiveLogin)
        {
            var args = new string[]
            { 
                name, alias, organizationUnit, upnName, samAccountName, firstName, middleInital, lastName, password, upnName, userRole, windowsLiveLogin
            };

            var command = new PSCommand();
            command.AddScript(string.Format(MailBoxScripts.CreateOnPremiseExchangeMailBoxScript, args));

            var accounts = this.sessionRunspace.ExecuteCommand(command);
            if (accounts != null && accounts.Count == 1)
            {
                return true;
            }

            return false;
        }
    }
}
